package com.cnbot.canapp.listener.healthy;

import android.util.Log;

import com.cnbot.canapp.listener.base.MultiSerialItem;
import com.cnbot.canapp.utils.ByteUtil;
import com.cnbot.canapp.utils.ICanCode;
import com.dc.android_can.can.CanDataType;

import java.text.DecimalFormat;



/**
 * 描述：
 * 作者：dc on 2018/6/20 14:09
 * 邮箱：597210600@qq.com
 */
public class HealthyItem extends MultiSerialItem<HealthyImp> {

	public HealthyItem(HealthyImp healthyImp) {
		super(healthyImp, ICanCode.BUF_TEMP_DATA_CODE, ICanCode.BUF_ECG_DATA_CODE, ICanCode.BUF_BLOOD_OXYGEN_DATA_CODE,
				ICanCode.BUF_BLOOD_PRESSURE_DATA_CODE, ICanCode.BUF_HEARTBEAT_CODE, ICanCode.BUF_BLOOD_SUGAR_DATA_CODE,
				ICanCode.BUF_BLOOD_SUGAR_PAPER_STATUS_CODE, ICanCode.BUF_BLOOD_SUGAR_BLOOD_STATUS_CODE,
				ICanCode.BUF_BLOOD_SUGAR_COUNT_DOWN_CODE, ICanCode.BUF_FILL_IN_LIGHT_CODE);
		Log.d(TAG, "HealthyItem: ");
	}

	@Override
	public void onOrderDispatch(int id) {

		if (id == ICanCode.BUF_INTERACTIVE_CODE && !canDataType.dir) {
			//            Log.e(TAG, "健康传感器板");
			// 健康传感器板
			healThyCanReadToken(canDataType);
		}
	}

	/**
	 * @descriptoin 处理所有指令
	 * @author dc
	 * @param canDataType
	 * @date 2018/11/8 14:52
	 */
	private void healThyCanReadToken(CanDataType canDataType) {
		//ww
		if (mListener == null)
			return;

		// 交互板
		if (canDataType.cmdWord == ICanCode.BUF_START_TEMP_CODE || canDataType.cmdWord == ICanCode.BUF_START_BLOOD_PRESSURE_CODE
				|| canDataType.cmdWord == ICanCode.BUF_START_BLOOD_OXYGEN_CODE || canDataType.cmdWord == ICanCode.BUF_START_BLOOD_SUGAR_CODE
				|| canDataType.cmdWord == ICanCode.BUF_FILL_IN_LIGHT_CODE || canDataType.cmdWord == ICanCode.BUF_START_ECG_CODE) {
			// TODO: 2018/11/14 dc 控制指令的回复:开始测量、控制指令回复（灯光...）
			mListener.tokenReplyListener(canDataType.cmdWord);

		} else if (canDataType.cmdWord == ICanCode.BUF_TEMP_DATA_CODE) {
			// 体温上报
			double temp = -1;
			if (canDataType.buf[1] == ICanCode.BUF_HEALTHY_ERROR_CODE) {
				Log.e(TAG, "healThyCanReadToken: 体温测量异常");
			} else {
				int count = ((canDataType.buf[1]) & 0xff) << 8 | (canDataType.buf[0] & 0xff);
				DecimalFormat df = new DecimalFormat("0.0");//格式化小数
				String num = df.format((float) (count - 10000) / 100);//返回的是String类型
				temp = Double.valueOf(num);
			}
			//            Log.e(TAG, "体温 = " + temp);
			mListener.tempListener(temp);
		} else if (canDataType.cmdWord == ICanCode.BUF_ECG_DATA_CODE) {
			// 心电上报
			double ecg = -1;
			if (canDataType.buf[1] != ICanCode.BUF_HEALTHY_ERROR_CODE) {
				ByteUtil.HexLog(canDataType.buf);
				ecg = ((canDataType.buf[1] << 8) & 0xff00) | ((canDataType.buf[0] & 0xff));
				Log.e(TAG, "心电 = " + ecg);
			} else {
				Log.e(TAG, "healThyCanReadToken: 心电上报异常");
			}
			mListener.ecgListener(ecg);
		} else if (canDataType.cmdWord == ICanCode.BUF_BLOOD_PRESSURE_DATA_CODE) {
			// 血压上报
			Log.e(TAG, "id = " + canDataType.id + "   len = " + canDataType.len + "   cmdWord = " + Integer.toHexString(canDataType.cmdWord)
					+ "   buf = " + canDataType.buf[0] + "  " + canDataType.buf[1] + "  " + canDataType.buf[2] + "  " + canDataType.buf[3]);
			double hp = -1, lp = -1, heart = -1;
			if (canDataType.buf[1] != ICanCode.BUF_HEALTHY_ERROR_CODE) {
				hp = (canDataType.buf[1] & 0xff);
				lp = (canDataType.buf[2] & 0xff);
				heart = (canDataType.buf[3] & 0xff);
			} else {
				Log.e(TAG, "healThyCanReadToken: 血压上报异常");
			}
			//            Log.e(TAG, "血压:   高压 = " + hp + "   低压 = " + lp + "   心率 = " + heart);
			mListener.bpListener(hp, lp, heart);
		} else if (canDataType.cmdWord == ICanCode.BUF_BLOOD_OXYGEN_DATA_CODE) {
			// 血氧上报
			double oxygen = -1, heart = -1;
			if (canDataType.buf[1] != ICanCode.BUF_HEALTHY_ERROR_CODE) {
				oxygen = (canDataType.buf[0] & 0xff);
				heart = (canDataType.buf[1] & 0xff);
				//                Log.e(TAG, "血氧 =   " + oxygen + "   心率 = " + heart);
			} else {
				Log.e(TAG, "healThyCanReadToken: 血氧上报异常");
			}
			mListener.boListener(oxygen, heart);
		} else if (canDataType.cmdWord == ICanCode.BUF_BLOOD_SUGAR_PAPER_STATUS_CODE) {
			// 血糖试纸插入状态上报
			//			mListener.bsStatusListener(ICanCode.BUF_BLOOD_SUGAR_PAPER_STATUS_CODE);
			//ww
			mListener.bsPaperInsertListener();
			Log.d(TAG, "血糖 血糖试纸插入状态上报");
		} else if (canDataType.cmdWord == ICanCode.BUF_BLOOD_SUGAR_BLOOD_STATUS_CODE) {
			// 血糖血液滴入状态上报
			//			mListener.bsStatusListener(ICanCode.BUF_BLOOD_SUGAR_BLOOD_STATUS_CODE);
			//ww
			mListener.bsBloodDripListener();
			Log.d(TAG, "血糖 血液滴入状态上报");
		} else if (canDataType.cmdWord == ICanCode.BUF_BLOOD_SUGAR_COUNT_DOWN_CODE) {
			// 血糖检测倒计时上报
			int down = (int) canDataType.buf[3];
			Log.d(TAG, "倒计时 = " + down);
			mListener.bsCountDownListener(down);
		} else if (canDataType.cmdWord == ICanCode.BUF_BLOOD_SUGAR_DATA_CODE) {
			// 血糖上报
			double sugar = -1;
			if (canDataType.buf[1] != ICanCode.BUF_HEALTHY_ERROR_CODE) {
				int count = ((canDataType.buf[1]) & 0xff) << 8 | (canDataType.buf[0] & 0xff);
				DecimalFormat df = new DecimalFormat("0.0");//格式化小数
				String num = df.format((float) (count) / 100);//返回的是String类型
				sugar = Double.valueOf(num);
			} else {
				Log.e(TAG, "healThyCanReadToken: 血糖上报异常");
			}
			mListener.bsResultListener(sugar);
			Log.d(TAG, "血糖 = " + sugar);
		} else if (canDataType.cmdWord == ICanCode.BUF_HEARTBEAT_CODE) {
			// 心跳上报
			//			            Log.v("XIN", "收到心跳数据");
			mListener.heartbeatListener();
		} else if (canDataType.cmdWord == ICanCode.BUF_STOP_MEASURE_CODE) {
			// 结束当前测量回复，会发送两次
			Log.d(TAG, "结束当前测量");
			mListener.stopMessaureListener();
		} else if (canDataType.cmdWord == ICanCode.BUF_BUTTON_SOSL_CODE) {
			Log.d(TAG, "按钮按下" + (byte) canDataType.cmdWord);
			mListener.onLeftSosKey();
			//ww
		} else if (canDataType.cmdWord == ICanCode.BUF_BUTTON_ONE_CODE) {

			mListener.onFirstKey();

		} else if (canDataType.cmdWord == ICanCode.BUF_BUTTON_TWO_CODE) {

			mListener.onSecondKey();

		} else if (canDataType.cmdWord == ICanCode.BUF_BUTTON_THREE_CODE) {

			mListener.onThirdKey();

		} else if (canDataType.cmdWord == ICanCode.BUF_BUTTON_FOUR_CODE) {

			mListener.onFourthKey();

		} else if (canDataType.cmdWord == ICanCode.BUF_BUTTON_FIVE_CODE) {

			mListener.onFifthKey();

		} else if (canDataType.cmdWord == ICanCode.BUF_BUTTON_SOSR_CODE) {

			mListener.onRightSosKey();

		}
	}
}
